﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Code.Models;
using Code.Commen;
using System.Security.Cryptography;

namespace Code.BLL
{
    public class AdminManage:ManageBase<Admin>
    {

        public LoginResult Login(string loginName, string password,string loginIP,out Admin admin)
        {
            var rs = LoginResult.SysError;

            admin = base.Select(s => s.FirstOrDefault(p => p.LoginName == loginName));

            if (admin == null)
            { rs = LoginResult.UnExist; }
            else if (admin.State == (short)AdminState.Locked)
            { rs = LoginResult.Locked; }
            else
            {
                MD5 md5 = MD5.Create();

                string pwd1 = md5.ComputeHash(password, admin.Salt);
                rs = admin.Password.Equals(pwd1) ? LoginResult.Normal : LoginResult.PWDError;
            }

            if (admin != null)
            {
                var loginLog = new
                {
                    AdminID = admin.ID,
                    LoginIP = loginIP,
                    LoginResult = (short)rs,
                    FailCause = rs.GetName()
                };
                base.Insert<LoginLog>(loginLog);
            }

            return rs;
        }

        /// <summary>
        /// 修改登陆密码
        /// </summary>
        /// <param name="checkpwd">验证登陆密码</param>
        public int EditPwd(Guid id,string newPassword)
        {
            int rs = -1;
            var a = base.Select(s => s.FirstOrDefault(p => p.ID == id));
            if (a == null)
                throw new ValidateException("不存在该信息", "id");
            MD5 md5 = MD5.Create();
            string Password = md5.ComputeHash(newPassword, a.Salt);
            rs = base.Update(new { Password }, new { ID = id });
            return rs;
        }

        public bool CheckPwd(Guid id, string password)
        {
            bool rs = false;
            var a = base.Select(s => s.FirstOrDefault(p => p.ID == id));
            if (a == null)
                throw new ValidateException("不存在该信息", "id");

            MD5 md5 = MD5.Create();
            string pwd = md5.ComputeHash(password, a.Salt);
            rs = pwd == a.Password;
            return rs;
        }

        public int EditState(Guid id, AdminState state)
        {
            int rs = 0;
            rs = base.Update(new { State = (short)state }, new { ID = id });
            return rs;
        }

        /// <summary>
        /// 修改管理员基本信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newPassword">密码为空则不修改</param>
        /// <param name="RoleID"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int Edit(Guid id, string newPassword, int RoleID, AdminState state)
        {
            var update = new NameValue<string, object>();
            if (!string.IsNullOrEmpty(newPassword))
            {
                var a = base.Select(s => s.FirstOrDefault(p => p.ID == id));
                if (a == null)
                    throw new ValidateException("不存在该信息", "id");
                MD5 md5 = MD5.Create();
                string Password = md5.ComputeHash(newPassword, a.Salt);
                update.Add("Password", Password);
            }
            update.Add("RoleID", RoleID);
            update.Add("State", (short)state);
            var where = new NameValue<string, object>
            {
                {"ID",id}
            };
            return base.Update(update, where);
        }

        public int Add(string loginName, string password,int roleid,AdminState state)
        {
            if (string.IsNullOrEmpty(loginName))
                throw new ValidateException("登陆名不能为空", "loginName");
            if (string.IsNullOrEmpty(password))
                throw new ValidateException("密码不能为空", "password");
            if(password.Length<6||password.Length>15)
                throw new ValidateException("密码在6-15位之间", "password");
            int rs = 0;
            string salt = Guid.NewGuid().ToString();
            Guid id = Guid.NewGuid();

            MD5 md5 = MD5.Create();
            string pwd = md5.ComputeHash(password, salt);
            rs = base.Insert(new { ID = id, LoginName = loginName, RoleID = roleid, Salt = salt,Password=pwd, State = (short)state });
            return rs;
        }
    }
}
